// pages/user/myorderdetail.js
var app = getApp();
var rootDocment = app.globalData.postUrl;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: {},
        detail: [],
        currentID: '',
        show: false,
        steps: [{
                text: '步骤一',
                desc: '描述信息',

            },
            {
                text: '步骤二',
                desc: '描述信息',

            },
            {
                text: '步骤三',
                desc: '描述信息',

            },
            {
                text: '步骤四',
                desc: '描述信息',

            },
        ],

        showWuliu: false,
        wuliuItem: {},
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        that.setData({
            currentID: options.id
        });
    },

    //下拉刷新
    onPullDownRefresh: function () {
        var that = this;
        this.setDetailData();
        wx.stopPullDownRefresh();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function (opt) {
        var that = this
        //用户授权登录
        app.login(function (userInfo) {
            that.setData({
                userInfo: userInfo
            });
            that.setDetailData();
        });
    },

    //初始化详情
    setDetailData: function () {
        var that = this;
        var paraArr = new Array();
        paraArr['id'] = that.data.currentID;
        paraArr['user_id'] = that.data.userInfo.userID;
        var sign = app.signature(paraArr);
        wx.request({
            url: rootDocment + '/api_order/' + paraArr['id'],
            data: {
                user_id: paraArr['user_id'],
                sign: sign
            },
            method: 'GET',
            header: {},
            success: function (res) {
                let list = []
                res.data._express_sn = []
                if (res.data.express_sn != '') {
                    list = res.data.express_sn.split(',')
                    list.forEach(o => {
                        res.data._express_sn.push({
                            name: o,
                            subname: res.data.express_title
                        })
                    })
                }

                that.setData({
                    detail: res.data
                });
            }
        })
    },

    //去付款
    goPay: function (e) {
        app.redirect('pay/index', 'sn=' + e.currentTarget.dataset.sn);
    },

    //去评价
    goComment: function (e) {
        app.redirect('user/mycomment', 'id=' + e.currentTarget.dataset.id);
    },

    //取消订单
    cancelOrder: function (e) {
        var that = this;
        var m_id = e.currentTarget.dataset.id
        if (!m_id) return;
        var paraArr = new Array();
        paraArr['id'] = m_id;
        paraArr['m_type'] = 'cancel';
        paraArr['user_id'] = that.data.userInfo.userID;
        var sign = app.signature(paraArr);
        wx.showModal({
            title: '提示',
            content: '确认要取消吗？',
            success: function (res) {
                if (res.confirm) {
                    wx.request({
                        url: rootDocment + '/api_order/' + m_id,
                        data: {
                            m_type: 'cancel',
                            user_id: paraArr['user_id'],
                            sign: sign
                        },
                        method: 'PUT',
                        header: {},
                        success: function (res) {
                            app.redirect('user/myorder', 'type=0');
                        }
                    })
                }
            }
        })
    },

    //确认收货
    shOrder: function (e) {
        var that = this;
        var m_id = e.currentTarget.dataset.id
        if (!m_id) return;
        var paraArr = new Array();
        paraArr['id'] = m_id;
        paraArr['m_type'] = 'received';
        paraArr['user_id'] = that.data.userInfo.userID;
        var sign = app.signature(paraArr);
        wx.showModal({
            title: '提示',
            content: '确认要收货吗？',
            success: function (res) {
                if (res.confirm) {
                    wx.request({
                        url: rootDocment + '/api_order/' + m_id,
                        data: {
                            m_type: paraArr['m_type'],
                            user_id: paraArr['user_id'],
                            sign: sign
                        },
                        method: 'PUT',
                        header: {},
                        success: function (res) {
                            app.redirect('user/myorder', 'type=3');
                        }
                    })
                }
            }
        })
    },

    //查看物流
    showSend: function (e) {
        console.log(e, 11111)
        var that = this;
        var m_id = e.currentTarget.dataset.id
        if (!m_id) return;
        wx.request({
            url: rootDocment + '/api/comm_get/getSend',
            data: {
                order_id: m_id
            },
            method: 'GET',
            header: {},
            success: function (res) {
                wx.showModal({
                    title: res.data.sendType,
                    content: res.data.sendNo,
                    showCancel: false,
                    success: function (res) {}
                })
            }
        })
    },
    //查看物流
    showSends: function (e) {
        let item = e.currentTarget.dataset.item

        if (item.kind) {
            wx.navigateTo({
                url: `/pages/orderTraceability/traceabilitydetail?id=${item.order_sn}&goodsType=${item.kind}`,
            })
        } else {
            this.setData({
                wuliuItem: item,
                showWuliu: true
            })
        }
    },

    // 物流选择关闭
    onCloseActionSheet() {
        this.setData({
            showWuliu: false
        })
    },

    // 物流选择
    onActionSheet(e) {
        let detail = e.detail
        let item = this.data.wuliuItem

        this.onCloseActionSheet()



        if (item.kind) {
            wx.navigateTo({
                url: `/pages/orderTraceability/traceabilitydetail?id=${item.order_sn}&goodsType=${item.kind}`,
            })
        } else {
            const now = new Date();
            const targetDate = new Date(item.push_time);
            const threeMonthsAgo = new Date(now.setMonth(now.getMonth() - 3));

            if (targetDate < threeMonthsAgo) {
                wx.navigateTo({
                    url: `/subpage/logistics_info/logistics_info?id=${item.express_sn}`,
                })

                return
            }

            wx.navigateTo({
                url: `plugin://kdzs-plugin/logistics-list?num=${detail.name}&phone=${item.tel.substring(7)}`,
            })
        }
    },


    // 关闭物流弹窗
    onClose() {
        this.setData({
            show: false
        });
    },

    //删除订单
    delOrder: function (e) {
        var that = this;
        var m_id = e.currentTarget.dataset.id
        if (!m_id) return;
        var paraArr = new Array();
        paraArr['id'] = m_id;
        paraArr['user_id'] = that.data.userInfo.userID;
        var sign = app.signature(paraArr);
        wx.showModal({
            title: '提示',
            content: '确认要删除吗？',
            success: function (res) {
                if (res.confirm) {
                    wx.request({
                        url: rootDocment + '/api_order/' + m_id,
                        data: {
                            user_id: paraArr['user_id'],
                            sign: sign
                        },
                        method: 'DELETE',
                        header: {},
                        success: function (res) {
                            app.redirect('user/myorder', 'type=-1');
                        }
                    })
                }
            }
        })
    },

})